home *** CD-ROM | disk | FTP | other *** search
- _C PROGRAMMING COLUMN_
- by Al Stevens
-
- [LISTING ONE]
-
- /* --------------- lists.c -------------- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <conio.h>
- #include <dos.h>
- #include "dflat.h"
-
- struct LinkedList Focus = {NULLWND, NULLWND};
- struct LinkedList Built = {NULLWND, NULLWND};
-
- /* --- set focus to the window beneath the one specified --- */
- void SetPrevFocus(WINDOW wnd)
- {
- if (wnd != NULLWND && wnd == inFocus) {
- WINDOW wnd1 = wnd;
- while (TRUE) {
- if ((wnd1 = PrevWindow(wnd1)) == NULLWND)
- wnd1 = Focus.LastWindow;
- if (wnd1 == wnd)
- return;
- if (wnd1 != NULLWND)
- break;
- }
- if (wnd1 != NULLWND)
- SendMessage(wnd1, SETFOCUS, TRUE, 0);
- }
- }
-
- /* this function assumes that wnd is in the Focus linked list */
- static WINDOW SearchFocusNext(WINDOW wnd, WINDOW pwnd)
- {
- WINDOW wnd1 = wnd;
-
- if (wnd != NULLWND) {
- while (TRUE) {
- if ((wnd1 = NextWindow(wnd1)) == NULLWND)
- wnd1 = Focus.FirstWindow;
- if (wnd1 == wnd)
- return NULLWND;
- if (wnd1 != NULLWND)
- if (pwnd == NULLWND || pwnd == GetParent(wnd1))
- break;
- }
- }
- return wnd1;
- }
-
- /* ----- set focus to the next sibling ----- */
- void SetNextFocus(WINDOW wnd)
- {
- WINDOW wnd1;
-
- if (wnd != inFocus)
- return;
- if ((wnd1 = SearchFocusNext(wnd, GetParent(wnd)))==NULLWND)
- wnd1 = SearchFocusNext(wnd, NULLWND);
- if (wnd1 != NULLWND)
- SendMessage(wnd1, SETFOCUS, TRUE, 0);
- }
-
- /* ---- remove a window from the Built linked list ---- */
- void RemoveBuiltWindow(WINDOW wnd)
- {
- if (wnd != NULLWND) {
- if (PrevWindowBuilt(wnd) != NULLWND)
- NextWindowBuilt(PrevWindowBuilt(wnd)) =
- NextWindowBuilt(wnd);
- if (NextWindowBuilt(wnd) != NULLWND)
- PrevWindowBuilt(NextWindowBuilt(wnd)) =
- PrevWindowBuilt(wnd);
- if (wnd == Built.FirstWindow)
- Built.FirstWindow = NextWindowBuilt(wnd);
- if (wnd == Built.LastWindow)
- Built.LastWindow = PrevWindowBuilt(wnd);
- }
- }
-
- /* ---- remove a window from the Focus linked list ---- */
- void RemoveFocusWindow(WINDOW wnd)
- {
- if (wnd != NULLWND) {
- if (PrevWindow(wnd) != NULLWND)
- NextWindow(PrevWindow(wnd)) = NextWindow(wnd);
- if (NextWindow(wnd) != NULLWND)
- PrevWindow(NextWindow(wnd)) = PrevWindow(wnd);
- if (wnd == Focus.FirstWindow)
- Focus.FirstWindow = NextWindow(wnd);
- if (wnd == Focus.LastWindow)
- Focus.LastWindow = PrevWindow(wnd);
- }
- }
-
- /* ---- append a window to the Built linked list ---- */
- void AppendBuiltWindow(WINDOW wnd)
- {
- if (wnd != NULLWND) {
- if (Built.FirstWindow == NULLWND)
- Built.FirstWindow = wnd;
- if (Built.LastWindow != NULLWND)
- NextWindowBuilt(Built.LastWindow) = wnd;
- PrevWindowBuilt(wnd) = Built.LastWindow;
- NextWindowBuilt(wnd) = NULLWND;
- Built.LastWindow = wnd;
- }
- }
-
- /* ---- append a window to the Focus linked list ---- */
- void AppendFocusWindow(WINDOW wnd)
- {
- if (wnd != NULLWND) {
- if (Focus.FirstWindow == NULLWND)
- Focus.FirstWindow = wnd;
- if (Focus.LastWindow != NULLWND)
- NextWindow(Focus.LastWindow) = wnd;
- PrevWindow(wnd) = Focus.LastWindow;
- NextWindow(wnd) = NULLWND;
- Focus.LastWindow = wnd;
- }
- }
-
- /* -------- get the first child of a parent window ------- */
- WINDOW GetFirstChild(WINDOW wnd)
- {
- WINDOW ThisWindow = Built.FirstWindow;
- while (ThisWindow != NULLWND) {
- if (GetParent(ThisWindow) == wnd)
- break;
- ThisWindow = NextWindowBuilt(ThisWindow);
- }
- return ThisWindow;
- }
-
- /* -------- get the next child of a parent window ------- */
- WINDOW GetNextChild(WINDOW wnd, WINDOW ThisWindow)
- {
- if (ThisWindow != NULLWND) {
- do {
- if ((ThisWindow = NextWindowBuilt(ThisWindow)) !=
- NULLWND)
- if (GetParent(ThisWindow) == wnd)
- break;
- } while (ThisWindow != NULLWND);
- }
- return ThisWindow;
- }
-
- /* -- get first child of parent window from the Focus list -- */
- WINDOW GetFirstFocusChild(WINDOW wnd)
- {
- WINDOW ThisWindow = Focus.FirstWindow;
- while (ThisWindow != NULLWND) {
- if (GetParent(ThisWindow) == wnd)
- break;
- ThisWindow = NextWindow(ThisWindow);
- }
- return ThisWindow;
- }
-
- /* -- get next child of parent window from the Focus list -- */
- WINDOW GetNextFocusChild(WINDOW wnd, WINDOW ThisWindow)
- {
- while (ThisWindow != NULLWND) {
- ThisWindow = NextWindow(ThisWindow);
- if (ThisWindow != NULLWND)
- if (GetParent(ThisWindow) == wnd)
- break;
- }
- return ThisWindow;
- }
-
- /* -------- get the last child of a parent window ------- */
- WINDOW GetLastChild(WINDOW wnd)
- {
- WINDOW ThisWindow = Built.LastWindow;
- while (ThisWindow != NULLWND) {
- if (GetParent(ThisWindow) == wnd)
- break;
- ThisWindow = PrevWindowBuilt(ThisWindow);
- }
- return ThisWindow;
- }
-
- /* ------- get the previous child of a parent window ------- */
- WINDOW GetPrevChild(WINDOW wnd, WINDOW ThisWindow)
- {
- if (ThisWindow != NULLWND) {
- do {
- if ((ThisWindow = PrevWindowBuilt(ThisWindow)) !=
- NULLWND)
- if (GetParent(ThisWindow) == wnd)
- break;
- } while (ThisWindow != NULLWND);
- }
- return ThisWindow;
- }
-
- /* --- bypass system windows when stepping through focus --- */
- void SkipSystemWindows(int Prev)
- {
- int cl, ct = 0;
- while ((cl = GetClass(inFocus)) == MENUBAR ||
- cl == APPLICATION
- #ifdef INCLUDE_STATUSBAR
- || cl == STATUSBAR
- #endif
- ) {
- if (Prev)
- SetPrevFocus(inFocus);
- else
- SetNextFocus(inFocus);
- if (++ct == 3)
- break;
- }
- }
-
-
- [LISTING TWO]
-
- /* ------------- rect.c --------------- */
-
- #include <dos.h>
- #include "dflat.h"
-
- /* -- Produce vector end points produced by overlap of two other vectors -- */
- static void subVector(int *v1, int *v2, int t1, int t2, int o1, int o2)
- {
- *v1 = *v2 = -1;
- if (within(o1, t1, t2)) {
- *v1 = o1;
- if (within(o2, t1, t2))
- *v2 = o2;
- else
- *v2 = t2;
- }
- else if (within(o2, t1, t2)) {
- *v2 = o2;
- if (within(o1, t1, t2))
- *v1 = o1;
- else
- *v1 = t1;
- }
- else if (within(t1, o1, o2)) {
- *v1 = t1;
- if (within(t2, o1, o2))
- *v2 = t2;
- else
- *v2 = o2;
- }
- else if (within(t2, o1, o2)) {
- *v2 = t2;
- if (within(t1, o1, o2))
- *v1 = t1;
- else
- *v1 = o1;
- }
- }
-
- /* -- Return rectangle produced by the overlap of two other rectangles --- */
- RECT subRectangle(RECT r1, RECT r2)
- {
- RECT r = {0,0,0,0};
- subVector((int *) &RectLeft(r), (int *) &RectRight(r),
- RectLeft(r1), RectRight(r1),
- RectLeft(r2), RectRight(r2));
- subVector((int *) &RectTop(r), (int *) &RectBottom(r),
- RectTop(r1), RectBottom(r1),
- RectTop(r2), RectBottom(r2));
- if (RectRight(r) == -1 || RectTop(r) == -1)
- RectRight(r) =
- RectLeft(r) =
- RectTop(r) =
- RectBottom(r) = 0;
- return r;
- }
-
- /* ------- return the client rectangle of a window ------ */
- RECT ClientRect(void *wnd)
- {
- RECT rc;
-
- RectLeft(rc) = GetClientLeft((WINDOW)wnd);
- RectTop(rc) = GetClientTop((WINDOW)wnd);
- RectRight(rc) = GetClientRight((WINDOW)wnd);
- RectBottom(rc) = GetClientBottom((WINDOW)wnd);
- return rc;
- }
-
- /* ---- return the rectangle relative to its window's screen position ---- */
- RECT RelativeWindowRect(void *wnd, RECT rc)
- {
- RectLeft(rc) -= GetLeft((WINDOW)wnd);
- RectRight(rc) -= GetLeft((WINDOW)wnd);
- RectTop(rc) -= GetTop((WINDOW)wnd);
- RectBottom(rc) -= GetTop((WINDOW)wnd);
- return rc;
- }
-
- /* ----- clip a rectangle to the parents of the window ----- */
- RECT ClipRectangle(void *wnd, RECT rc)
- {
- RECT sr;
- RectLeft(sr) = RectTop(sr) = 0;
- RectRight(sr) = SCREENWIDTH-1;
- RectBottom(sr) = SCREENHEIGHT-1;
- if (!TestAttribute((WINDOW)wnd, NOCLIP))
- while ((wnd = GetParent((WINDOW)wnd)) != NULLWND)
- rc = subRectangle(rc, ClientRect(wnd));
- return subRectangle(rc, sr);
- }
-
-
-
-
- [LISTING THREE]
-
- /* ----------- dflatmsg.h ------------ */
-
- /* message foundation file
- * make message changes here
- * other source files will adapt
- */
-
- /* -------------- process communication messages ----------- */
- DFlatMsg(START) /* start message processing */
- DFlatMsg(STOP) /* stop message processing */
- DFlatMsg(COMMAND) /* send a command to a window */
- /* -------------- window management messages --------------- */
- DFlatMsg(CREATE_WINDOW) /* create a window */
- DFlatMsg(SHOW_WINDOW) /* show a window */
- DFlatMsg(HIDE_WINDOW) /* hide a window */
- DFlatMsg(CLOSE_WINDOW) /* delete a window */
- DFlatMsg(SETFOCUS) /* set and clear the focus */
- DFlatMsg(PAINT) /* paint the window's data space*/
- DFlatMsg(BORDER) /* paint the window's border */
- DFlatMsg(TITLE) /* display the window's title */
- DFlatMsg(MOVE) /* move the window */
- DFlatMsg(SIZE) /* change the window's size */
- DFlatMsg(MAXIMIZE) /* maximize the window */
- DFlatMsg(MINIMIZE) /* minimize the window */
- DFlatMsg(RESTORE) /* restore the window */
- DFlatMsg(INSIDE_WINDOW) /* test x/y inside a window */
- /* ---------------- clock messages ------------------------- */
- DFlatMsg(CLOCKTICK) /* the clock ticked */
- DFlatMsg(CAPTURE_CLOCK) /* capture clock into a window */
- DFlatMsg(RELEASE_CLOCK) /* release clock to the system */
- /* -------------- keyboard and screen messages ------------- */
- DFlatMsg(KEYBOARD) /* key was pressed */
- DFlatMsg(CAPTURE_KEYBOARD) /* capture keyboard into a window */
- DFlatMsg(RELEASE_KEYBOARD) /* release keyboard to system */
- DFlatMsg(KEYBOARD_CURSOR) /* position the keyboard cursor */
- DFlatMsg(CURRENT_KEYBOARD_CURSOR) /*read the cursor position */
- DFlatMsg(HIDE_CURSOR) /* hide the keyboard cursor */
- DFlatMsg(SHOW_CURSOR) /* display the keyboard cursor */
- DFlatMsg(SAVE_CURSOR) /* save the cursor's configuration*/
- DFlatMsg(RESTORE_CURSOR) /* restore the saved cursor */
- DFlatMsg(SHIFT_CHANGED) /* the shift status changed */
- DFlatMsg(WAITKEYBOARD) /* waits for a key to be released */
- /* ---------------- mouse messages ------------------------- */
- DFlatMsg(MOUSE_INSTALLED) /* test for mouse installed */
- DFlatMsg(RIGHT_BUTTON) /* right button pressed */
- DFlatMsg(LEFT_BUTTON) /* left button pressed */
- DFlatMsg(DOUBLE_CLICK) /* left button double-clicked */
- DFlatMsg(MOUSE_MOVED) /* mouse changed position */
- DFlatMsg(BUTTON_RELEASED) /* mouse button released */
- DFlatMsg(CURRENT_MOUSE_CURSOR)/* get mouse position */
- DFlatMsg(MOUSE_CURSOR) /* set mouse position */
- DFlatMsg(SHOW_MOUSE) /* make mouse cursor visible */
- DFlatMsg(HIDE_MOUSE) /* hide mouse cursor */
- DFlatMsg(WAITMOUSE) /* wait until button released */
- DFlatMsg(TESTMOUSE) /* test any mouse button pressed*/
- DFlatMsg(CAPTURE_MOUSE) /* capture mouse into a window */
- DFlatMsg(RELEASE_MOUSE) /* release the mouse to system */
- /* ---------------- text box messages ---------------------- */
- DFlatMsg(ADDTEXT) /* add text to the text box */
- DFlatMsg(CLEARTEXT) /* clear the edit box */
- DFlatMsg(SETTEXT) /* set address of text buffer */
- DFlatMsg(SCROLL) /* vertical scroll of text box */
- DFlatMsg(HORIZSCROLL) /* horizontal scroll of text box*/
- /* ---------------- edit box messages ---------------------- */
- DFlatMsg(EB_GETTEXT) /* get text from an edit box */
- DFlatMsg(EB_PUTTEXT) /* put text into an edit box */
- /* ---------------- menubar messages ----------------------- */
- DFlatMsg(BUILDMENU) /* build the menu display */
- DFlatMsg(SELECTION) /* menubar selection */
- /* ---------------- popdown messages ----------------------- */
- DFlatMsg(BUILD_SELECTIONS) /* build the menu display */
- DFlatMsg(CLOSE_POPDOWN) /* tell parent popdown is closing */
- /* ---------------- list box messages ---------------------- */
- DFlatMsg(LB_SELECTION) /* sent to parent on selection */
- DFlatMsg(LB_CHOOSE) /* sent when user chooses */
- DFlatMsg(LB_CURRENTSELECTION)/* return the current selection */
- DFlatMsg(LB_GETTEXT) /* return the text of selection */
- DFlatMsg(LB_SETSELECTION) /* sets the listbox selection */
- /* ---------------- dialog box messages -------------------- */
- DFlatMsg(INITIATE_DIALOG) /* begin a dialog */
- DFlatMsg(ENTERFOCUS) /* tell DB control got focus */
- DFlatMsg(LEAVEFOCUS) /* tell DB control lost focus */
- DFlatMsg(ENDDIALOG) /* end a dialog */
- /* ---------------- help box messages ---------------------- */
- DFlatMsg(DISPLAY_HELP)
- /* --------------- application window messages ------------- */
- DFlatMsg(ADDSTATUS)
-
-
-
- [LISTING FOUR]
-
- /* ---------------- commands.h ----------------- */
-
- /* Command values sent as the first parameter
- * in the COMMAND message
- * Add application-specific commands to this enum
- */
-
- #ifndef COMMANDS_H
- #define COMMANDS_H
-
- enum commands {
- /* --------------- File menu ---------------- */
- ID_OPEN,
- ID_NEW,
- ID_SAVE,
- ID_SAVEAS,
- ID_DELETEFILE,
- ID_PRINT,
- ID_DOS,
- ID_EXIT,
- /* --------------- Edit menu ---------------- */
- ID_UNDO,
- ID_CUT,
- ID_COPY,
- ID_PASTE,
- ID_PARAGRAPH,
- ID_CLEAR,
- ID_DELETETEXT,
- /* --------------- Search Menu -------------- */
- ID_SEARCH,
- ID_REPLACE,
- ID_SEARCHNEXT,
- /* -------------- Options menu -------------- */
- ID_INSERT,
- ID_WRAP,
- ID_LOG,
- ID_TABS,
- ID_DISPLAY,
- ID_SAVEOPTIONS,
- /* --------------- Window menu -------------- */
- ID_WINDOW,
- ID_CLOSEALL,
- /* --------------- Help menu ---------------- */
- ID_HELPHELP,
- ID_EXTHELP,
- ID_KEYSHELP,
- ID_HELPINDEX,
- ID_ABOUT,
- ID_LOADHELP,
- /* --------------- System menu -------------- */
- ID_SYSRESTORE,
- ID_SYSMOVE,
- ID_SYSSIZE,
- ID_SYSMINIMIZE,
- ID_SYSMAXIMIZE,
- ID_SYSCLOSE,
- /* ---- FileOpen and SaveAs dialog boxes ---- */
- ID_FILENAME,
- ID_FILES,
- ID_DRIVE,
- ID_PATH,
- /* ----- Search and Replace dialog boxes ---- */
- ID_SEARCHFOR,
- ID_REPLACEWITH,
- ID_MATCHCASE,
- ID_REPLACEALL,
- /* ----------- Windows dialog box ----------- */
- ID_WINDOWLIST,
- /* --------- generic command buttons -------- */
- ID_OK,
- ID_CANCEL,
- ID_HELP,
- /* -------------- TabStops menu ------------- */
- ID_TAB2,
- ID_TAB4,
- ID_TAB6,
- ID_TAB8,
- /* ------------ Display dialog box ---------- */
- ID_BORDER,
- ID_TITLE,
- ID_STATUSBAR,
- ID_TEXTURE,
- ID_COLOR,
- ID_MONO,
- ID_REVERSE,
- ID_25LINES,
- ID_43LINES,
- ID_50LINES,
- /* ------------- Log dialog box ------------- */
- ID_LOGLIST,
- ID_LOGGING,
- /* ------------ HelpBox dialog box ---------- */
- ID_HELPTEXT,
- ID_BACK,
- ID_PREV,
- ID_NEXT
- };
-
- #endif
-
-
-
-
- [LISTING FIVE]
-
- /* --------------- memopad.c ----------- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys\types.h>
- #include <sys\stat.h>
- #include <dos.h>
- #ifndef MSC
- #include <dir.h>
- #endif
- #include "dflat.h"
-
- static char Untitled[] = "Untitled";
- static int wndpos = 0;
-
- static int MemoPadProc(WINDOW, MESSAGE, PARAM, PARAM);
- static void NewFile(WINDOW);
- static void SelectFile(WINDOW);
- static void PadWindow(WINDOW, char *);
- static void OpenPadWindow(WINDOW, char *);
- static void LoadFile(WINDOW, int);
- static void PrintPad(WINDOW);
- static void SaveFile(WINDOW, int);
- static void DeleteFile(WINDOW);
- static int EditorProc(WINDOW, MESSAGE, PARAM, PARAM);
- static char *NameComponent(char *);
- char **Argv;
-
- void main(int argc, char *argv[])
- {
- WINDOW wnd;
- init_messages();
- Argv = argv;
- wnd = CreateWindow(APPLICATION,
- "D-Flat MemoPad " VERSION,
- 0, 0, -1, -1,
- &MainMenu,
- NULL,
- MemoPadProc,
- MOVEABLE |
- SIZEABLE |
- HASBORDER |
- HASSTATUSBAR
- );
-
- SendMessage(wnd, SETFOCUS, TRUE, 0);
- while (argc > 1) {
- PadWindow(wnd, argv[1]);
- --argc;
- argv++;
- }
- while (dispatch_message())
- ;
- }
- /* ------ open text files and put them into editboxes ----- */
- static void PadWindow(WINDOW wnd, char *FileName)
- {
- int ax, criterr = 1;
- struct ffblk ff;
- char path[64];
- char *cp;
-
- CreatePath(path, FileName, FALSE, FALSE);
- cp = path+strlen(path);
- CreatePath(path, FileName, TRUE, FALSE);
- while (criterr == 1) {
- ax = findfirst(path, &ff, 0);
- criterr = TestCriticalError();
- }
- while (ax == 0 && !criterr) {
- strcpy(cp, ff.ff_name);
- OpenPadWindow(wnd, path);
- ax = findnext(&ff);
- }
- }
- /* ----- window processing module for the memopad application window ----- */
- static int MemoPadProc(WINDOW wnd,MESSAGE msg,PARAM p1,PARAM p2)
- {
- switch (msg) {
- case COMMAND:
- switch ((int)p1) {
- case ID_NEW:
- NewFile(wnd);
- return TRUE;
- case ID_OPEN:
- SelectFile(wnd);
- return TRUE;
- case ID_SAVE:
- SaveFile(inFocus, FALSE);
- return TRUE;
- case ID_SAVEAS:
- SaveFile(inFocus, TRUE);
- return TRUE;
- case ID_DELETEFILE:
- DeleteFile(inFocus);
- return TRUE;
- case ID_PRINT:
- PrintPad(inFocus);
- return TRUE;
- case ID_ABOUT:
- MessageBox(
- "About D-Flat and the MemoPad",
- " ┌───────────────────────┐\n"
- " │ ▄▄▄ ▄▄▄ ▄ │\n"
- " │ █ █ █ █ █ │\n"
- " │ █ █ █ █ █ │\n"
- " │ █ █ █ █ █ █ │\n"
- " │ ▀▀▀ ▀▀▀ ▀▀ │\n"
- " └───────────────────────┘\n"
- "D-Flat implements the SAA/CUA\n"
- "interface in a public domain\n"
- "C language library originally\n"
- "published in Dr. Dobb's Journal\n"
- " ------------------------ \n"
- "MemoPad is a multiple document\n"
- "editor that demonstrates D-Flat");
- MessageBox(
- "D-Flat Testers and Friends",
- "Jeff Ratcliff, David Peoples, Kevin Slater,\n"
- "Naor Toledo Pinto, Jeff Hahn, Jim Drash,\n"
- "Art Stricek, John Ebert, George Dinwiddie,\n"
- "Damaian Thorne, Wes Peterson, Thomas Ewald,\n"
- "Mitch Miller, Ray Waters, Jim Drash, Eric\n"
- "Silver, Russ Nelson, Elliott Jackson, Warren\n"
- "Master, H.J. Davey, Jim Kyle, Jim Morris,\n"
- "Andrew Terry, Michel Berube, Bruce Edmondson,\n"
- "Peter Baenziger, Phil Roberge, Willie Hutton,\n"
- "Randy Bradley, Tim Gentry, Lee Humphrey,\n"
- "Larry Troxler, Robert DiFalco, Carl Huff,\n"
- "Vince Rice, Michael Kaufman, Donald Cumming,\n"
- "Ross Wheeler, Lou DiNardo, Keith London, Frank\n"
- "Burleigh, Jason Ward, Skip Key, Sam Gentile,\n"
- "Dana P'Simer, Ken North");
- return TRUE;
- default:
- break;
- }
- break;
- default:
- break;
- }
- return DefaultWndProc(wnd, msg, p1, p2);
- }
- /* --- The New command. Open an empty editor window --- */
- static void NewFile(WINDOW wnd)
- {
- OpenPadWindow(wnd, Untitled);
- }
- /* --- The Open... command. Select a file --- */
- static void SelectFile(WINDOW wnd)
- {
- char FileName[64];
- if (OpenFileDialogBox("*.PAD", FileName)) {
- /* --- see if the document is already in a window --- */
- WINDOW wnd1 = GetFirstChild(wnd);
- while (wnd1 != NULLWND) {
- if (stricmp(FileName, wnd1->extension) == 0) {
- SendMessage(wnd1, SETFOCUS, TRUE, 0);
- SendMessage(wnd1, RESTORE, 0, 0);
- return;
- }
- wnd1 = GetNextChild(wnd, wnd1);
- }
- OpenPadWindow(wnd, FileName);
- }
- }
- /* --- open a document window and load a file --- */
- static void OpenPadWindow(WINDOW wnd, char *FileName)
- {
- static WINDOW wnd1 = NULLWND;
- struct stat sb;
- char *Fname = FileName;
- char *ermsg;
- if (strcmp(FileName, Untitled)) {
- if (stat(FileName, &sb)) {
- if ((ermsg = malloc(strlen(FileName)+20)) != NULL) {
- strcpy(ermsg, "No such file as\n");
- strcat(ermsg, FileName);
- ErrorMessage(ermsg);
- free(ermsg);
- }
- return;
- }
- Fname = NameComponent(FileName);
- }
- wndpos += 2;
- if (wndpos == 20)
- wndpos = 2;
- wnd1 = CreateWindow(EDITBOX,
- Fname,
- (wndpos-1)*2, wndpos, 10, 40,
- NULL, wnd, EditorProc,
- SHADOW |
- MINMAXBOX |
- CONTROLBOX |
- VSCROLLBAR |
- HSCROLLBAR |
- MOVEABLE |
- HASBORDER |
- SIZEABLE |
- MULTILINE
- );
- if (strcmp(FileName, Untitled)) {
- if ((wnd1->extension =
- malloc(strlen(FileName)+1)) != NULL) {
- strcpy(wnd1->extension, FileName);
- LoadFile(wnd1, (int) sb.st_size);
- }
- }
- SendMessage(wnd1, SETFOCUS, TRUE, 0);
- }
- /* --- Load the notepad file into the editor text buffer --- */
- static void LoadFile(WINDOW wnd, int tLen)
- {
- char *Buf;
- FILE *fp;
-
- if ((Buf = malloc(tLen+1)) != NULL) {
- if ((fp = fopen(wnd->extension, "rt")) != NULL) {
- memset (Buf, 0, tLen+1);
- fread(Buf, tLen, 1, fp);
- SendMessage(wnd, SETTEXT, (PARAM) Buf, 0);
- fclose(fp);
- }
- free(Buf);
- }
- }
- /* --- print the current notepad --- */
- static void PrintPad(WINDOW wnd)
- {
- unsigned char *text;
-
- /* ---------- print the file name ---------- */
- fputs("\r\n", stdprn);
- fputs(GetTitle(wnd), stdprn);
- fputs(":\r\n\n", stdprn);
-
- /* ---- get the address of the editor text ----- */
- text = GetText(wnd);
-
- /* ------- print the notepad text --------- */
- while (*text) {
- if (*text == '\n')
- fputc('\r', stdprn);
- fputc(*text++, stdprn);
- }
-
- /* ------- follow with a form feed? --------- */
- if (YesNoBox("Form Feed?"))
- fputc('\f', stdprn);
- }
- /* ---------- save a file to disk ------------ */
- static void SaveFile(WINDOW wnd, int Saveas)
- {
- FILE *fp;
- if (wnd->extension == NULL || Saveas) {
- char FileName[64];
- if (SaveAsDialogBox(FileName)) {
- if (wnd->extension != NULL)
- free(wnd->extension);
- if ((wnd->extension =
- malloc(strlen(FileName)+1)) != NULL) {
- strcpy(wnd->extension, FileName);
- AddTitle(wnd, NameComponent(FileName));
- SendMessage(wnd, BORDER, 0, 0);
- }
- }
- else
- return;
- }
- if (wnd->extension != NULL) {
- WINDOW mwnd = MomentaryMessage("Saving the file");
- if ((fp = fopen(wnd->extension, "wt")) != NULL) {
- fwrite(GetText(wnd), strlen(GetText(wnd)), 1, fp);
- fclose(fp);
- wnd->TextChanged = FALSE;
- }
- SendMessage(mwnd, CLOSE_WINDOW, 0, 0);
- }
- }
- /* -------- delete a file ------------ */
- static void DeleteFile(WINDOW wnd)
- {
- if (wnd->extension != NULL) {
- if (strcmp(wnd->extension, Untitled)) {
- char *fn = NameComponent(wnd->extension);
- if (fn != NULL) {
- char msg[30];
- sprintf(msg, "Delete %s?", fn);
- if (YesNoBox(msg)) {
- unlink(wnd->extension);
- SendMessage(wnd, CLOSE_WINDOW, 0, 0);
- }
- }
- }
- }
- }
- /* ------ display the row and column in the statusbar ------ */
- static void ShowPosition(WINDOW wnd)
- {
- char status[30];
- sprintf(status, "Line:%4d Column: %2d",
- wnd->CurrLine, wnd->CurrCol);
- SendMessage(GetParent(wnd), ADDSTATUS, (PARAM) status, 0);
- }
- /* ----- window processing module for the editboxes ----- */
- static int EditorProc(WINDOW wnd,MESSAGE msg,PARAM p1,PARAM p2)
- {
- int rtn;
- switch (msg) {
- case SETFOCUS:
- rtn = DefaultWndProc(wnd, msg, p1, p2);
- if ((int)p1 == FALSE)
- SendMessage(GetParent(wnd), ADDSTATUS, 0, 0);
- else
- ShowPosition(wnd);
- return rtn;
- case KEYBOARD_CURSOR:
- rtn = DefaultWndProc(wnd, msg, p1, p2);
- ShowPosition(wnd);
- return rtn;
- case COMMAND:
- if ((int) p1 == ID_HELP) {
- DisplayHelp(wnd, "MEMOPADDOC");
- return TRUE;
- }
- break;
- case CLOSE_WINDOW:
- if (wnd->TextChanged) {
- char *cp = malloc(25+strlen(GetTitle(wnd)));
- SendMessage(wnd, SETFOCUS, TRUE, 0);
- if (cp != NULL) {
- strcpy(cp, GetTitle(wnd));
- strcat(cp, "\nText changed. Save it?");
- if (YesNoBox(cp))
- SendMessage(GetParent(wnd),
- COMMAND, ID_SAVE, 0);
- free(cp);
- }
- }
- wndpos = 0;
- if (wnd->extension != NULL) {
- free(wnd->extension);
- wnd->extension = NULL;
- }
- break;
- default:
- break;
- }
- return DefaultWndProc(wnd, msg, p1, p2);
- }
- /* -- point to the name component of a file specification -- */
- static char *NameComponent(char *FileName)
- {
- char *Fname;
- if ((Fname = strrchr(FileName, '\\')) == NULL)
- if ((Fname = strrchr(FileName, ':')) == NULL)
- Fname = FileName-1;
- return Fname + 1;
- }
-
-
-
-
- Examplσ 1:
-
- WINDOW cwnd = GetFirstChild(pwnd);
- while (cwnd != NULLWND) {
- /* -- process the child window -- */
- cwnd = GetNextChild(pwnd, cwnd);
- è }
-
-
- Examplσ 2:
-
-
- typedef enum messages {
- #undef DFlatMsg
- #define DFlatMsg(m) m,
- #include "dflatmsg.h"
- MESSAGECOUNT
- } MESSAGE;
-
-
-
-
- Examplσ 3
-
- static char *message[] = {
- #undef DFlatMsg
- #define DFlatMsg(m) " " #m,
- #include "dflatmsg.h"
- NULL
- è };